package com.wispx.shortlink.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wispx.shortlink.project.dao.entity.ShortLinkDO;
import com.wispx.shortlink.project.dao.mapper.ShortLinkMapper;
import com.wispx.shortlink.project.dto.req.RecycleBinPageReqDTO;
import com.wispx.shortlink.project.dto.req.RecycleBinRecoverReqDTO;
import com.wispx.shortlink.project.dto.req.RecycleBinRemoveReqDTO;
import com.wispx.shortlink.project.dto.req.RecycleBinSaveReqDTO;
import com.wispx.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import com.wispx.shortlink.project.service.RecycleBinService;
import com.wispx.shortlink.project.toolkit.LinkUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import static com.wispx.shortlink.project.common.constant.RedisKeyConstant.GOTO_SHORT_LINK_KEY;
import static com.wispx.shortlink.project.common.constant.RedisKeyConstant.IS_NULL_GOTO_SHORT_LINK;

/**
 * 回收站接口实现层
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<ShortLinkMapper,ShortLinkDO> implements RecycleBinService {

    private final StringRedisTemplate stringRedisTemplate;

    @Override
    public void saveRecycleBin(RecycleBinSaveReqDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getEnableStatus, 0);
        ShortLinkDO shortLinkDO = ShortLinkDO.builder()
                .enableStatus(1)
                .build();
        baseMapper.update(shortLinkDO,updateWrapper);
        stringRedisTemplate.delete(GOTO_SHORT_LINK_KEY + requestParam.getFullShortUrl());
    }

    @Override
    public IPage<ShortLinkPageRespDTO> pageShortLink(RecycleBinPageReqDTO requestParam) {
        LambdaQueryWrapper<ShortLinkDO> queryWrapper = Wrappers.lambdaQuery(ShortLinkDO.class)
                .in(ShortLinkDO::getGid, requestParam.getGidList())
                .eq(ShortLinkDO::getDelTime,0L)
                .eq(ShortLinkDO::getEnableStatus, 1);
        IPage<ShortLinkDO> result = baseMapper.selectPage(requestParam, queryWrapper);
        return result.convert(each -> {
            ShortLinkPageRespDTO bean = BeanUtil.toBean(each, ShortLinkPageRespDTO.class);
            bean.setFullShortUrl("http://" + bean.getFullShortUrl());
            return bean;
        });
    }

    @Override
    public void recoverRecycleBin(RecycleBinRecoverReqDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getDelTime,0L)
                .eq(ShortLinkDO::getEnableStatus, 1);
        ShortLinkDO shortLinkDO = ShortLinkDO.builder()
                .enableStatus(0)
                .build();
        int updateCount = baseMapper.update(shortLinkDO, updateWrapper);
        if(updateCount < 1){
            return;
        }
        // 缓存预热
        stringRedisTemplate.opsForValue().set(GOTO_SHORT_LINK_KEY + requestParam.getFullShortUrl(),shortLinkDO.getOriginUrl(),
                LinkUtil.getValidTime(shortLinkDO.getValidDate()), TimeUnit.MILLISECONDS);
        // 删除空值
        stringRedisTemplate.delete(IS_NULL_GOTO_SHORT_LINK + requestParam.getFullShortUrl());
    }

    @Override
    public void removeRecycleBin(RecycleBinRemoveReqDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getDelTime,0L)
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getEnableStatus,1);
        ShortLinkDO delShortDO = ShortLinkDO.builder()
                .delTime(System.currentTimeMillis())
                .build();
        // 从回收站删除短链接就是 设置del_time,逻辑删除字段
        baseMapper.update(delShortDO,updateWrapper);
    }
}
